package com.yumeng.framework.auth.shiro.config;

import com.yumeng.framework.auth.service.TokenService;
import com.yumeng.framework.auth.service.impl.JwtTokenServiceImpl;
import com.yumeng.framework.auth.shiro.authc.AuthcStrategy;
import com.yumeng.framework.auth.shiro.filter.JwtFilter;
import com.yumeng.framework.auth.shiro.filter.JwtLoginFilter;
import jakarta.servlet.Filter;
import org.apache.commons.lang3.StringUtils;
import org.springframework.context.annotation.Bean;

import java.util.LinkedHashMap;

/**
 *
 * @author wxd
 * @date 2025/9/30 15:38
 */
public abstract class ShiroConfigurationForJson extends ShiroConfiguration{

    /** 获取token接口名 */
    protected String getTokenActionName(){
        return "/auth/getToken.do";
    }

    protected boolean isFBS(){
        return true;
    }

    @Bean
    public JwtTokenServiceImpl tokenService(){
        return new JwtTokenServiceImpl();
    }

    @Override
    protected AuthcStrategy getAuthcStrategy() {
        return AuthcStrategy.FirstSucc;
    }

    /** 是否启用session */
    @Override
    protected boolean enableSession() {
        return false;
    }

    /** 最后一级过滤器名称 */
    @Override
    protected String getLastFilterName() {
        return JwtFilter.FILTER_NAME;//authc authcBasic authcBearer
    }

    @Override
    protected TokenService getTokenService() {
        return tokenService();
    }

    @Override
    LinkedHashMap<String, String> getFilterChainDefinitionMapForJson() {
        LinkedHashMap<String,String> filterMap = new LinkedHashMap<String,String>();
        if (StringUtils.isNotBlank(getTokenActionName())){
            filterMap.put(getTokenActionName(), JwtLoginFilter.FILTER_NAME);
        }
        return filterMap;
    }

    @Override
    LinkedHashMap<String, Filter> getFilterMapForJson() {
        LinkedHashMap<String, Filter> map = new LinkedHashMap<>();
        JwtLoginFilter loginFilter = new JwtLoginFilter(getTokenService());
        loginFilter.setUsernameParameter(yumengAuthProperties.getBasic().getUsernameParameter());
        loginFilter.setPasswordParameter(yumengAuthProperties.getBasic().getPasswordParameter());
        loginFilter.setClientTypeParameter(yumengAuthProperties.getBasic().getClientTypeParameter());
        loginFilter.setJsonParam(false);//后期考虑在yumengAuthProperties中配置
        loginFilter.setPostOnly(false);
        map.put(JwtLoginFilter.FILTER_NAME, loginFilter);
        JwtFilter jwtFilter = new JwtFilter(getTokenService());
        map.put(JwtFilter.FILTER_NAME, jwtFilter);
        return map;
    }
}
